const express = require("express");
const cors = require("cors");
const session = require("express-session");
const app = express();
app.use(cors());
app.use(
  session({
    secret: "secret", // 秘钥
    resave: true,
    saveUninitialized: true,
  })
);

app.use(express.json()); //         解析json格式
app.use(express.urlencoded({ extended: true })); // 解析post请求

app.get("/api/users", (req, res) => {
  console.log(123, "/api/users");
  res.json({
    success: true,
    data: [
      {
        id: 1,
        name: "John Doe",
        email: "john@example.com",
        password: "123456",
      },
      {
        id: 2,
        name: "Jane Doe",
        email: "jane@example.com",
        password: "123456",
      },
    ],
  });
});

// 登录
app.post("/api/login", (req, res) => {
  console.log(req, "api/login");
  const  user = req.body;
  req.session.user = user;
  res.json({
    success: true,
    data: user,
  });
});

app.get("/api/login", (req, res) => {
  console.log(req, "api/login");
  const  user = req.body;
  req.session.user = user;
  res.json({
    success: true,
    data: user,
  });
});
 // 登出
app.get("/api/logout", (req, res) => {
  console.log(req, "api/logout");
  const  user = req.body;
  req.session.user = null;
  res.json({
    success: true,
    data: user,
  });
});

app.listen(3009, () => {
  console.log("Server is running on port 3000");
});
